package com.ithemai.work;

import java.io.*;

/**
 * @auther Macrydy
 * @date 2020/7/4 18:35
 *  3.1 题目一
 * 	请定义Student类，有以下属性：姓名、性别、年龄，并实现Serializable接口。
 * 	请定义main()方法，创建一个Student对象：
 * 		Student stu = new Student(“迪丽热巴”,”女”,18);
 * 		//定义一个序列化对象：ObjectOutputStream，将stu序列化到文件“test5_1.txt”中
 * 	3.2 题目二
 *  * 	请定义main()方法，创建一个反序列化对象：ObjectInputStream，将文件”test5_1.txt”的对象反序列化到程序中，并打印此对象
 */
public class Test6 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Student1 stu = new Student1("迪丽热巴","女",18);
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("test5_1.txt"));
        oos.writeObject(stu);
        oos.close();


        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test5_1.txt"));
        Object o = ois.readObject();
        System.out.println(o);
    }
}
class Student1 implements Serializable {
    private String name;
    private String sex;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Student1(String name, String sex, int age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student1{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                '}';
    }

    public Student1() {
    }
}